package com.yxx.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.yxx.common.annotation.RepeatSubmit;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.annotation.Log;
import com.yxx.common.core.controller.BaseControllerPlus;
import com.yxx.common.enums.BusinessType;
import com.yxx.business.entity.TbPersonnelEmissionDo;
import com.yxx.business.entity.TbPersonnelEmissionVo;
import com.yxx.business.entity.TbPersonnelEmissionQueryDto;
import com.yxx.business.entity.TbPersonnelEmissionEditDto;
import com.yxx.business.service.ITbPersonnelEmissionService;
import com.yxx.common.utils.poi.ExcelUtil;
import com.yxx.common.core.domain.R;
import com.yxx.common.core.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.validation.annotation.Validated;


/**
 * 人员排放Controller
 *
 * @author yxx
 * @date 2025-08-16
 */
@Tag(name = "人员排放管理-TbPersonnelEmission")
@RestController
@RequestMapping("/business/persionEmission")
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbPersonnelEmissionController extends BaseControllerPlus {

    private final ITbPersonnelEmissionService tbPersonnelEmissionService;

    @Operation(summary = "查询--人员排放列表")
    @GetMapping("/list")
    public PageResult<TbPersonnelEmissionVo> list(@ParameterObject TbPersonnelEmissionQueryDto tbPersonnelEmissionQueryDto) {
        return tbPersonnelEmissionService.selectTbPersonnelEmissionVoPage(tbPersonnelEmissionQueryDto);
    }

    @Operation(summary = "查询--人员排放单个")
    @GetMapping(value = "/{personnelId}")
    public R<TbPersonnelEmissionVo> getInfo(@PathVariable Long personnelId) {
        return R.ok(tbPersonnelEmissionService.selectTbPersonnelEmissionVoByPersonnelId(personnelId));
    }

    @Operation(summary = "新增--人员排放")
    @Log(title = "人员排放", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<String> add(@RequestBody @Validated TbPersonnelEmissionEditDto tbPersonnelEmissionQueryDto) {
        return toResult(tbPersonnelEmissionService.insertTbPersonnelEmission(tbPersonnelEmissionQueryDto));
    }

    @Operation(summary = "修改--人员排放")
    @Log(title = "人员排放", businessType = BusinessType.UPDATE)
    @PutMapping("/{personnelId}")
    @RepeatSubmit(interval = 2000, message = "您操作的太频繁了，结果正在保存中，请稍后重试")
    public R<Integer> update(@PathVariable Long personnelId, @RequestBody @Validated TbPersonnelEmissionEditDto tbPersonnelEmissionQueryDto) {
        return R.ok(tbPersonnelEmissionService.updateTbPersonnelEmission(personnelId, tbPersonnelEmissionQueryDto));
    }

    @Operation(summary = "删除--人员排放")
    @Log(title = "人员排放", businessType = BusinessType.DELETE)
    @DeleteMapping("/{personnelIds}")
    public R<String> delete(@PathVariable List<Long> personnelIds) {
        return toResult(tbPersonnelEmissionService.deleteTbPersonnelEmissionByPersonnelIds(personnelIds));
    }

    @Operation(summary = "导出--人员排放列表")
    @PreAuthorize("@ss.hasPermi('business:persionEmission:export')")
    @Log(title = "人员排放", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ParameterObject TbPersonnelEmissionQueryDto tbPersonnelEmissionQueryDto) {
        List<TbPersonnelEmissionDo> list = tbPersonnelEmissionService.selectTbPersonnelEmissionDoList(tbPersonnelEmissionQueryDto);
        ExcelUtil<TbPersonnelEmissionDo> util = new ExcelUtil<>(TbPersonnelEmissionDo.class);
        util.exportExcel(response, list, "人员排放数据");
    }

    @Operation(summary = "导出--人员排放模板")
    @PreAuthorize("@ss.hasPermi('business:persionEmission:import')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        ExcelUtil<TbPersonnelEmissionDo> util = new ExcelUtil<>(TbPersonnelEmissionDo.class);
        util.importTemplateExcel(response, "导入人员排放模板数据");
    }

    @Operation(summary = "导入--人员排放列表")
    @PreAuthorize("@ss.hasPermi('business:persionEmission:import')")
    @Log(title = "人员排放", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Boolean> importData(MultipartFile file) throws IOException {
        ExcelUtil<TbPersonnelEmissionDo> util = new ExcelUtil<>(TbPersonnelEmissionDo.class);
        List<TbPersonnelEmissionDo> list = util.importExcel(file.getInputStream());
        return R.ok(tbPersonnelEmissionService.saveBatch(list), "成功导入 " + list.size() + " 条记录");
    }
}
